#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QPushButton>
#include <QFileDialog>
#include <QDesktopServices>
#include <QUrl>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_inputButton_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(nullptr, "选择文件夹", QDir::homePath());
    if (dir.isEmpty()) {
       return ;
    }

    qDebug() << "input_path" << dir;

    this->input_path = dir;

    ui->inputButton->setEnabled(false);

    ui->inputlabel->setText(this->input_path);

}

void Widget::on_intputcomboBox_currentIndexChanged(int index)
{
    qDebug() << "input_type" << index;

    ui->inputcomboBox->setEnabled(false);

    this->input_type = index;
}

void Widget::on_intputSortcomboBox_currentIndexChanged(int index)
{
    qDebug() << "input_sort" << index;

    ui->inputSortcomboBox->setEnabled(false);

    this->input_sort = index;
}

void Widget::on_set_total_file(int val)
{
    this->total_file = val;
}

void Widget::file_searchDirectory(const QString &path)
{
    int total = 0;
    QString file ;
    QDir dir(path);

    if (!dir.exists()) {
        qDebug() << "The directory does not exist!";
        return;
    }

    dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); // 设置过滤器来获取所有文件和目录
    dir.setSorting(QDir::Size | QDir::Reversed); // 可选的排序方式

    QFileInfoList list = dir.entryInfoList(); // 获取目录内容的列表
    for (int i = 0; i < list.size(); ++i)
    {
        QFileInfo fileInfo = list.at(i);

        if (fileInfo.isDir()) {
            file_searchDirectory(fileInfo.filePath()); // 递归搜索子目录
        }else{
            file = path + "/" + fileInfo.fileName();
            total++;

            qDebug() <<"路径:"<< file;
        }
    }

    this->on_set_total_file(total);

    QString str1 = QString("文件总数:%1").arg(total);
    
    ui->fileTotallabel->setText(str1);

    qDebug() << "total_file" << total << endl;
}

void Widget::on_StartpushButton_clicked()
{
    qDebug() << "start" << endl;

    this->total_file  = 0;

    ui->StartpushButton->setEnabled(false);

    file_searchDirectory(this->input_path);

}
